package ar.Location.PlaceObject;

import java.util.ArrayList;

import android.util.Log;
import ar.Location.PlaceApi.AbsAPI;
import ar.activity.DataFilterActivity;
import ar.activity.DataSourceActivity;

public class PlaceCategory {

	private static final String TAG = "PlaceCategory";

	public int categoryIndex = -1;
	public String categoryName = "";

	public boolean finishLoading = false;

	public AbsAPI placeAPI = null;

	public static Place currentPlace = null;
	public ArrayList<Place> listPlaceResult = null;

	Thread threadLoadByPoint = null;
	Thread threadLoadByType = null;

	public PlaceCategory(int index, String filterName, AbsAPI fPlaceAPI) {
		placeAPI = fPlaceAPI;
		categoryIndex = index;
		categoryName = filterName;
	}

	public Place getPlaceAtIndex(int position) {
		if (position >= 0 && position < listPlaceResult.size()) {
			if (listPlaceResult != null && listPlaceResult.size() > 0) {
				return listPlaceResult.get(position);
			}
		}
		return null;
	}

	public int count() {
		if (listPlaceResult != null && listPlaceResult.size() > 0) {
			return listPlaceResult.size();
		}
		return 0;
	}

	public boolean isFinishLoading() {
		return finishLoading;
	}

	public boolean isProcessingLoadByPoint() {
		if (threadLoadByPoint != null) {
			if (threadLoadByPoint.isAlive()) {
				return true;
			}
		}
		return false;
	}

	public boolean isProcessingLoadByType() {
		if (threadLoadByType != null) {
			if (threadLoadByType.isAlive()) {
				return true;
			}
		}
		return false;
	}

	public void loadListPlaces(final String latitude, final String longitude,
			final int radiusMetters) {
		threadLoadByPoint = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				Log.d(TAG, "Start load by point");
				finishLoading = false;
				listPlaceResult = placeAPI.getListPlacesByGeoPoint(latitude,
						longitude, radiusMetters);

				Log.d(TAG, "Finish load data source");

				finishLoading = true;
				if (DataSourceActivity.handlerUpdateListView != null) {
					synchronized (DataSourceActivity.handlerUpdateListView) {
						DataSourceActivity.handlerUpdateListView
								.sendEmptyMessage(0);
					}
				}
			}
		});
		threadLoadByPoint.start();
	}

	public void loadListPlaces(final String latitude, final String longitude,
			final String type, final int radiusMetters) {
		threadLoadByType = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				Log.d(TAG, "Start load by type");

				listPlaceResult = placeAPI.getListPlacesByType(latitude,
						longitude, type, radiusMetters);

				Log.d(TAG, "Finish load categories");

				finishLoading = true;

				if (DataFilterActivity.handlerUpdateGridView != null) {
					synchronized (DataFilterActivity.handlerUpdateGridView) {
						DataFilterActivity.handlerUpdateGridView
								.sendEmptyMessage(0);
					}
				}
			}
		});
		threadLoadByType.start();
	}

	public void clear() {
		try {
			if (listPlaceResult == null) {
				listPlaceResult = new ArrayList<Place>();
			} else {
				if (listPlaceResult.size() > 0) {
					int count = listPlaceResult.size();
					for (int i = 0; i < count; i++) {
						if (listPlaceResult.get(i) != null) {
							finishLoading = false;
						}
					}
					listPlaceResult.clear();
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			String msg = e.getMessage();
			if (msg != null) {
				Log.d(TAG, msg);
			}
		}
	}
}
